/*
 * @lc app=leetcode.cn id=232 lang=typescript
 *
 * [232] 用栈实现队列
 */

// @lc code=start
//  将一个栈当作输入栈，用于压入数据；
//  另一个栈当作输出栈，用于 pop 和 peek 操作。
//  每次 pop 或 peek 时，若输出栈为空则将输入栈的数据倒进输出栈，使得输出栈保持队列的顺序。

//  复杂度： O(1)  O(n)

class MyQueue {
    inStack: number[]
    outStack: number[]
    constructor() {
        this.inStack = []
        this.outStack = []
    }

    push(x: number): void {
        this.inStack.push(x)
    }

    pop(): number {
        this.peek()
        return this.outStack.pop()!
    }

    peek(): number {
        if (!this.outStack.length) {
            while (this.inStack.length) {
                this.outStack.push(this.inStack.pop()!)
            }
        }
        return this.outStack[this.outStack.length - 1]
    }

    empty(): boolean {
        return !this.outStack.length && !this.inStack.length
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */
// @lc code=end
